package com.linewell.service.impl;

import com.linewell.dao.TagMapper;
import com.linewell.entity.Article;
import com.linewell.entity.Tag;
import com.linewell.service.ITagService;
import com.linewell.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Title: ssm2
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/2/5
 */
@Service
public class TagService implements ITagService{

    @Autowired
    TagMapper tagDao;

    public List<Tag> getHotTag() {
        return tagDao.getHotTag();
    }

    public Msg getTags() {
        List<Tag> tagList = tagDao.getTags();
        if (tagList.size()>0){
            return Msg.success().add("tagList",tagList);
        } else {
            return Msg.fail();
        }
    }

    public void insertTag(String tag) {
        tagDao.insertTag(tag);
    }

    public List<Tag> selectTagByTagName(String tag) {
        return tagDao.selectTagByTagName(tag);
    }

    public void insertMutil(Long userId, Long articleId, int tagId) {
        tagDao.insertMutil(articleId,tagId);
        int count =  tagDao.selectUserTag(userId);
        if (count < 3) {
            tagDao.insertUserTag(userId,tagId);
        } else {
            Long[] tId = tagDao.selectTagId(userId);
            tagDao.updateUserTag(tId[0],tagId);
        }
    }

    public void insertMutilByArticle(long articleId, int tagId) {
        tagDao.insertMutilByArticle(articleId,tagId);
    }

    public List<Tag> getTagByArticle(Article article) {
        return tagDao.getTagByArticle(article);
    }
}
